import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

/**
 * 注意：子菜单仅在路由children.length> = 1时出现
 *
 * hidden: true                   如果设置true, 不会显示在侧边栏中（默认为false）
 * alwaysShow: true               如果设置true, 将始终显示根菜单
 *                                如果未设置alwaysShow，则当项有多个子路由时，
 *                                它将变为嵌套模式，否则不显示根菜单
 * Redirect: noRedirect           如果设置noRedirect，则不会在面包屑中重定向
 * name:'routerName'              用来使用<keep-alive>
 * meta : {
    title: 'title'                显示在侧边栏和面包屑的名称（推荐设置）
    icon: 'svg-name'              显示在侧边栏的icon
    noCache: true                 如果设置为true，将不缓存页面（默认为false）
    affix: true                   如果设置为true，则标签将固定在tags-view中
    breadcrumb: false             如果设置为false，在面包屑中不显示（默认为true）
    activeMenu: '/example/list'   如果设置了路径，则侧边栏将高亮显示所设置的路径
  }
 */

/**
 * **constantRoutes**
 * 没有权限要求的基本页面
 * 所有角色都可以访问
 */

export const constantRoutes = [
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/index"),
    hidden: true,
    props: true,
  },

  {
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true,
  },
  {
    path: "/",
    redirect: "/home",
    hidden: true,
  },
  {
    path: "/index",
    component: Layout,
    children: [
      {
        path: "index",
        name: "index",
        component: () => import("@/views/index/index"),
        meta: { title: "首页", icon: "el-icon-link" },
      },
    ],
  },
  {
    path: "/home",
    name: "home",
    component: () => import("@/views/home/index"),
    hidden: true,
  },

  // {
  //   path: "/common",
  //   component: Layout,
  //   children: [
  //     {
  //       path: "index",
  //       name: "Common",
  //       component: () => import("@/views/home/components/notice.vue"),
  //       meta: { title: "公告", icon: "el-icon-link" },
  //     },
  //   ],
  // },
  // {
  //   path: "/noticeManage",
  //   component: Layout,
  //   children: [
  //     {
  //       path: "index",
  //       name: "noticeManage",
  //       component: () => import("@/views/noticeManage/index.vue"),
  //       meta: { title: "公告管理", icon: "el-icon-link" },
  //     },
  //   ],
  // },

  //管理员
  {
    path: "/userManage",
    component: Layout,
    children: [
      {
        path: "index",
        name: "userManage",
        component: () => import("@/views/userManage/index.vue"),
        meta: { title: "用户管理", icon: "el-icon-link" },
      },
    ],
  },
  {
    path: "/categoryManage",
    component: Layout,
    children: [
      {
        path: "index",
        name: "categoryManage",
        component: () => import("@/views/categoryManage/index.vue"),
        meta: { title: "分类管理", icon: "el-icon-link" },
      },
    ],
  },
  {
    path: "/productManage",
    component: Layout,
    children: [
      {
        path: "index",
        name: "productManage",
        component: () => import("@/views/productManage/index.vue"),
        meta: { title: "商品管理", icon: "el-icon-link" },
      },
    ],
  },
  {
    path: "/orderManage",
    component: Layout,
    children: [
      {
        path: "index",
        name: "orderManage",
        component: () => import("@/views/orderManage/index.vue"),
        meta: { title: "订单管理", icon: "el-icon-link" },
      },
    ],
  },
  {
    path: "/dataAnalyse",
    component: Layout,
    children: [
      {
        path: "index",
        name: "dataAnalyse",
        component: () => import("@/views/dataAnalyse/index.vue"),
        meta: { title: "数据分析", icon: "el-icon-link" },
      },
    ],
  },
  // 404 page must be placed at the end !!!
  { path: "*", redirect: "/404", hidden: true },
];

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465

// 这里将其matcher属性设置为newRouter.matcher。在Vue Router中，matcher是用于解析URL并将其实例化成一个Route对象的核心部分。通过替换现有的matcher，我们可以实现路由规则的刷新或重置，使得所有已注册过的路由恢复到初始状态（即不包含任何动态添加或修改过的路由信息）。
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
